﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class individual : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);
        }
        bool check = false;
        string name_tocheck1=this.Request.Params["q"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand sc_user = new SqlCommand();
        //从数据库中查找输入的用户名是否存在,若不存在将label1设置成提示信息，结束函数体，若存在继续
        sc_user.CommandText = "select IND_ID from INFO_IND where IND_NAME=@name";
        sc_user.Parameters.AddWithValue("@name", name_tocheck1); 
        sc_user.Connection = sc;
        if (sc_user.ExecuteScalar() == null)
        {
            check = false;
        }
        else check = true;  
        if (check == false)
        {
            Label1.Text = "未找到该用户。";
            GridView1.Visible = false;
            Label2.Visible = false;
        }
        else
        {
            int credits = 0;
            bool vip = false;
            //用户积分从用户信息表中获得，用用户名匹配得到，VIP信息根据平台ID 和用户ID 在IND_CARE关系表中获得。平台ID利用
            //Session["USERNAME"]在ENTER_INFO表中查询得到。
            SqlCommand com_credits = new SqlCommand();
            com_credits.Connection = sc;
            com_credits.CommandText = "select IND_CREDITS from INFO_IND where IND_NAME=@name";
            com_credits.Parameters.AddWithValue("@name",name_tocheck1);
            credits =Convert.ToInt32(com_credits.ExecuteScalar());

            SqlCommand com_vip = new SqlCommand();
            com_vip.Connection = sc;
            com_vip.CommandText = "select VIP from IND_CARE,INFO_IND,INFO_ENTER where INFO_IND.IND_ID=IND_CARE.IND_ID and INFO_ENTER.ENTER_ID=IND_CARE.ENTER_ID and INFO_IND.IND_NAME=@name and INFO_ENTER.ENTER_NAME=@name1";
            com_vip.Parameters.AddWithValue("@name",name_tocheck1);
            com_vip.Parameters.AddWithValue("@name1",Session["USERNAME"].ToString());
            int vip_check =Convert.ToInt32(com_vip.ExecuteScalar());
            if (vip_check == 1)
                vip = true;
            else vip = false;
            Label1.Text = "用户名： " + name_tocheck1 + "\n"+"积分：" + credits + "\n"+"是否为VIP: " + vip.ToString() + "\n";
            
            SqlCommand sc1 = new SqlCommand();
            string sqlStr = "select INFO_IND.IND_NAME,PRODUCT_SALE.IND_ID,INFO_PRODUCT.PRODUCT_NAME,PRODUCT_SALE.PRODUCT_ID,PRODUCT_SALE.SUM,PRODUCT_SALE.SALE_DATE from PRODUCT_SALE,INFO_PRODUCT,INFO_IND where PRODUCT_SALE.IND_ID=(select IND_ID from INFO_IND where IND_NAME=@name) and INFO_IND.IND_ID=PRODUCT_SALE.IND_ID and INFO_PRODUCT.PRODUCT_ID=PRODUCT_SALE.PRODUCT_ID and PRODUCT_SALE.PRODUCT_ID in(select INFO_PRODUCT.PRODUCT_ID from INFO_PRODUCT where INFO_PRODUCT.ENTER_ID=@eid1)";
            //以用户名在用户表中获得用户ID在销售记录表中获得消费记录导入
            sc1.CommandText = sqlStr;
            sc1.Connection = sc;
            sc1.Parameters.AddWithValue("@name",name_tocheck1);
            sc1.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
            SqlDataReader reader = sc1.ExecuteReader();
            GridView1.DataSource = reader;
            GridView1.DataBind();
            sc.Close();
        }

    }
    
}